package com.pcafs.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.pcafs.manage.bo.ParentInfoEditBo;
import com.pcafs.manage.domain.ParentInfo;
import com.pcafs.manage.mapper.ParentInfoMapper;
import com.pcafs.manage.vo.ParentInfoVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.pcafs.manage.bo.ParentInfoAddBo;
import com.pcafs.manage.bo.ParentInfoQueryBo;
import com.pcafs.manage.service.IParentInfoService;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 父母信息Service业务层处理
 *
 * @author pcafs
 * @date 2021-05-05
 */
@Service
public class ParentInfoServiceImpl extends ServiceImpl<ParentInfoMapper, ParentInfo> implements IParentInfoService {

    @Override
    public ParentInfoVo queryById(Long id){
        ParentInfo db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, ParentInfoVo.class);
    }

    @Override
    public List<ParentInfoVo> queryList(ParentInfoQueryBo bo) {
        LambdaQueryWrapper<ParentInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getStuId()), ParentInfo::getStuId, bo.getStuId());
        lqw.like(StrUtil.isNotBlank(bo.getFName()), ParentInfo::getFName, bo.getFName());
        lqw.like(StrUtil.isNotBlank(bo.getMName()), ParentInfo::getMName, bo.getMName());
        lqw.eq(StrUtil.isNotBlank(bo.getFUnit()), ParentInfo::getFUnit, bo.getFUnit());
        lqw.eq(StrUtil.isNotBlank(bo.getMUnit()), ParentInfo::getMUnit, bo.getMUnit());
        lqw.eq(StrUtil.isNotBlank(bo.getFPhone()), ParentInfo::getFPhone, bo.getFPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getMPhone()), ParentInfo::getMPhone, bo.getMPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getFJob()), ParentInfo::getFJob, bo.getFJob());
        lqw.eq(StrUtil.isNotBlank(bo.getMJob()), ParentInfo::getMJob, bo.getMJob());
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<ParentInfoVo> entity2Vo(Collection<ParentInfo> collection) {
        List<ParentInfoVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, ParentInfoVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<ParentInfo> page = (Page<ParentInfo>)collection;
            Page<ParentInfoVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(ParentInfoAddBo bo) {
        ParentInfo add = BeanUtil.toBean(bo, ParentInfo.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(ParentInfoEditBo bo) {
        ParentInfo update = BeanUtil.toBean(bo, ParentInfo.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ParentInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
